package ru.kruzhki.client.ui.searchform;
import ru.kruzhki.client.gwtp.AbstractView;
import ru.kruzhki.client.ui.searchform.SearchFormPresenter.MyViewHandler;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.inject.Inject;
import com.sencha.gxt.core.client.util.ToggleGroup;
import com.sencha.gxt.data.shared.StringLabelProvider;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Slider;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.CheckBox;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.DoublePropertyEditor;
import com.sencha.gxt.widget.core.client.form.Radio;
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;
import com.sencha.gxt.widget.core.client.form.SpinnerField;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.info.Info;

public class SearchFormView extends AbstractView<SimpleContainer> implements SearchFormPresenter.MyView {

	private MyViewHandler viewHandler;

	@Inject
	public SearchFormView() {
	}


	
	@Override
	protected SimpleContainer createWidget() {
	 	ContentPanel widget = new ContentPanel();
		widget.setHeaderVisible(false);
		widget.setBorders(true);
		widget.getBody().setBorders(false);

	    
	    widget.setBodyStyle("background: none; padding: 5px");
	    widget.setLayoutData(new VerticalLayoutData(1, 1));
	 
	    VerticalLayoutContainer p = new VerticalLayoutContainer();
	    widget.add(p);
	 
	    TextField clubName = new TextField();
	    p.add(new FieldLabel(clubName, "Название"), new VerticalLayoutData(1, -1));
	 
	 
	    SimpleComboBox<String> metro = new SimpleComboBox<String>(new StringLabelProvider<String>());
	    metro.add("Пионерская");
	    metro.add("Просвещение");
	    metro.add("Озерки");
	    metro.add("Удельная");
	    //metro.setEditable(false);
	    metro.setForceSelection(true);
	 
	    p.add(new FieldLabel(metro, "Метро"), new VerticalLayoutData(1, -1));
	 
	    Slider slider = new Slider();
	    slider.setMinValue(1);
	    slider.setMaxValue(70);
	    slider.setValue(10);
	    slider.setIncrement(1);
	    slider.setMessage("{0} лет");
	    p.add(new FieldLabel(slider, "Возраст"), new VerticalLayoutData(1, -1));
	     
	    ValueChangeHandler<Boolean> musicHandler = new ValueChangeHandler<Boolean>() {
	       
	      @Override
	      public void onValueChange(ValueChangeEvent<Boolean> event) {
	        CheckBox check = (CheckBox)event.getSource();
	        Info.display("Music Changed", check.getBoxLabel() + " " + (event.getValue() ? "selected" : "not selected"));
	      }
	    };
	 
	    CheckBox check1 = new CheckBox();
	    check1.setEnabled(false);
	    check1.setBoxLabel("Музыка");
	    check1.addValueChangeHandler(musicHandler);
	 
	    CheckBox check2 = new CheckBox();
	    check2.setBoxLabel("Спорт");
	    check2.addValueChangeHandler(musicHandler);
	    check2.setValue(true);
	 
	    CheckBox check3 = new CheckBox();
	    check3.setBoxLabel("Рисование");
	    check3.addValueChangeHandler(musicHandler);
	 
	    HorizontalPanel hp = new HorizontalPanel();
	    hp.add(check1);
	    hp.add(check2);
	    hp.add(check3);
	 
	    p.add(new FieldLabel(hp, "Тип занятий"));
	 
	    Radio radio = new Radio();
	    radio.setBoxLabel("Red");
	 
	    Radio radio2 = new Radio();
	    radio2.setBoxLabel("Blue");
	    radio2.setValue(true);
	 
	    hp = new HorizontalPanel();
	    hp.add(radio);
	    hp.add(radio2);
	 
	    p.add(new FieldLabel(hp, "Color"));
	 
	    // we can set name on radios or use toggle group
	    ToggleGroup toggle = new ToggleGroup();
	    toggle.add(radio);
	    toggle.add(radio2);
	    toggle.addValueChangeHandler(new ValueChangeHandler<HasValue<Boolean>>() {
	 
	      @Override
	      public void onValueChange(ValueChangeEvent<HasValue<Boolean>> event) {
	        ToggleGroup group = (ToggleGroup)event.getSource();
	        Radio radio = (Radio)group.getValue();
	        Info.display("Color Changed", "You selected " + radio.getBoxLabel());
	      }
	    });
	 
	 
	    final SpinnerField<Double> spinnerField = new SpinnerField<Double>(new DoublePropertyEditor());
	    spinnerField.setIncrement(.1d);
	    spinnerField.setMinValue(-10d);
	    spinnerField.setMaxValue(10d);
	    spinnerField.setAllowNegative(true);
	    spinnerField.setAllowBlank(false);
	    spinnerField.getPropertyEditor().setFormat(NumberFormat.getFormat("0.0"));
	    spinnerField.addValueChangeHandler(new ValueChangeHandler<Double>() {
	 
	      @Override
	      public void onValueChange(ValueChangeEvent<Double> event) {
	        Info.display("Duration Changed",
	            "Duration changed to " + event.getValue() == null ? "nothing" : event.getValue() + "");
	      }
	    });
	    spinnerField.addSelectionHandler(new SelectionHandler<Double>() {
	 
	      @Override
	      public void onSelection(SelectionEvent<Double> event) {
	        String msg = "nothing";
	        if (event.getSelectedItem() != null) {
	          msg = spinnerField.getPropertyEditor().render(event.getSelectedItem());
	        }
	 
	        Info.display("Spin Change", "Current value changed to " + msg);
	      }
	    });
	 
	    FieldLabel spinLabel = new FieldLabel(spinnerField, "Duration(s)");
	 
	    p.add(spinLabel, new VerticalLayoutData(1, -1));
	 
	    TextButton searchButton = new TextButton("Подобрать кружок");
	    searchButton.addSelectHandler(new SelectHandler() {
			@Override
			public void onSelect(SelectEvent event) {
				if(viewHandler!=null){
					viewHandler.onSearchClick();
				}
			}
		});
	    widget.addButton(searchButton);
	 
	    return widget;
	    
	}
	
	@Override
	public void setViewHandler(SearchFormPresenter.MyViewHandler viewHandler){
		this.viewHandler = viewHandler;
	}
		 
}
